<template>
  <div class="fixed-container">
    <el-container>
      <!-- 头部 -->
      <el-header style="display: flex; justify-content: space-between; align-items: center;">
        <div class="gradient-text">视频管理系统</div>
        <div>
          <el-row :gutter="10" style="margin-top: 5px; display: flex; align-items: center;">
            <!-- 搜索框 -->
            <div style="display: flex; align-items: center; gap: 10px; flex: 1;">
              <el-input 
                v-model="searchKeyword" 
                placeholder="请输入视频名称" 
                style="flex: 1;" 
                @keyup.enter.native="handleSearch">
              </el-input>
              <el-button type="primary" @click="handleSearch">搜索</el-button>
            </div>

            <!-- 用户信息 -->
            <div style="display: flex; align-items: center; gap: 10px; margin-left: 20px;">
              <div class="demo-basic--circle" @click="login">
                <div class="block">
                  <el-avatar :size="50" :src="circleUrl"></el-avatar>
                </div>
              </div>
              <div style="text-align: right;">
                <el-button v-if="!isLoggedIn" type="text" @click="login">登录</el-button>
                <span v-else>{{ username }}</span>
              </div>
            </div>
          </el-row>

          <!-- 登录对话框 -->
          <el-dialog title="登录" :visible.sync="loginDialogVisible" width="30%">
            <div style="display: flex; flex-direction: column; gap: 15px;">
              <el-input v-model="form.username" placeholder="请输入账号"></el-input>
              <el-input v-model="form.password" placeholder="请输入密码" show-password></el-input>
              <div style="display: flex; justify-content: center; gap: 20px;">
                <el-button @click="submitLogin">登录</el-button>
                <el-button @click="openRegisterDialog">注册</el-button>
              </div>
            </div>
          </el-dialog>

          <!-- 注册对话框 -->
          <el-dialog title="注册" :visible.sync="registerDialogVisible" width="30%">
            <div style="display: flex; flex-direction: column; gap: 15px;">
              <el-input v-model="form.username" placeholder="请输入账号"></el-input>
              <el-input v-model="form.password" placeholder="请输入密码" show-password></el-input>
              <el-input v-model="form.confirmPassword" placeholder="请确认密码" show-password></el-input>
              <el-button @click="register">提交注册</el-button>
            </div>
          </el-dialog>

          <!-- 提示对话框 -->
          <el-dialog :title="alertTitle" :visible.sync="alertDialogVisible" width="30%">
            <p>{{ alertMessage }}</p>
          </el-dialog>
        </div>
      </el-header>

      <!-- 主体容器 -->
      <el-container>
        <!-- 侧边栏菜单 -->
        <el-aside width="270px">
          <el-menu
            default-active="2"
            class="el-menu-vertical-demo"
            @open="handleOpen"
            @close="handleClose"
            background-color="#2c3e50"
            text-color="#ecf0f1"
            active-text-color="#3498db"
            :router="true">
            <el-submenu index="sy">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>首页</span>
              </template>
              <el-menu-item index="rmsp">热门电视剧</el-menu-item>
              <el-menu-item index="jxsp">精选电影</el-menu-item>
            </el-submenu>
            <el-menu-item index="hy">
              <i class="el-icon-star-on"></i>
              <span slot="title">会员</span>
            </el-menu-item>
            <el-menu-item index="lt">
              <i class="el-icon-document"></i>
              <span slot="title">论坛</span>
            </el-menu-item>
            <el-submenu index="wd">
              <template slot="title">
                <i class="el-icon-user"></i>
                <span>我的</span>
              </template>
              <el-menu-item index="grxx">个人信息</el-menu-item>
              <el-menu-item index="sc">上传</el-menu-item>
              <el-menu-item index="ck">存库</el-menu-item>
            </el-submenu>
            <el-menu-item index="sz">
              <i class="el-icon-s-tools"></i>
              <span slot="title">设置</span>
            </el-menu-item>
          </el-menu>
        </el-aside>

        <!-- 主内容区域 -->
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'HomeView',
  data() {
    return {
      searchKeyword: '',
      circleUrl: "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png",
      isLoggedIn: false,
      username: "",
      loginDialogVisible: false,
      registerDialogVisible: false,
      alertDialogVisible: false,
      alertTitle: "",
      alertMessage: "",
      form: {
        username: "",
        password: "",
        confirmPassword: ""
      },
      users: {}
    };
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    handleSearch() {
      const keyword = this.searchKeyword.trim();
      if (keyword) {
        this.$router.push({ path: '/rmsp', query: { keyword } });
      }
    },
    login() {
      this.loginDialogVisible = true;
    },
    submitLogin() {
      const { username, password } = this.form;

      if (!username || !password) {
        this.alertTitle = "错误";
        this.alertMessage = "请输入账号和密码";
        this.alertDialogVisible = true;
        return;
      }

      if (this.users[username]) {
        if (this.users[username] === password) {
          this.isLoggedIn = true;
          this.username = username;
          this.loginDialogVisible = false;
        } else {
          this.alertTitle = "错误";
          this.alertMessage = "密码错误";
          this.alertDialogVisible = true;
        }
      } else {
        this.alertTitle = "没有账号";
        this.alertMessage = "快去注册";
        this.alertDialogVisible = true;
      }
    },
    openRegisterDialog() {
      this.loginDialogVisible = false;
      this.registerDialogVisible = true;
    },
    register() {
      const { username, password, confirmPassword } = this.form;

      if (!username || !password || !confirmPassword) {
        this.alertTitle = "错误";
        this.alertMessage = "所有字段都必须填写";
        this.alertDialogVisible = true;
        return;
      }

      if (password !== confirmPassword) {
        this.alertTitle = "错误";
        this.alertMessage = "两次输入的密码不一致";
        this.alertDialogVisible = true;
        return;
      }

      if (this.users[username]) {
        this.alertTitle = "错误";
        this.alertMessage = "该账号已存在";
        this.alertDialogVisible = true;
      } else {
        this.users[username] = password;
        this.alertTitle = "成功";
        this.alertMessage = "注册成功，请登录";
        this.alertDialogVisible = true;
        this.registerDialogVisible = false;
        this.loginDialogVisible = true;
      }
    },
    handleLogout(command) {
      if (command === "logout") {
        this.isLoggedIn = false;
        this.username = "";
      }
    },
    beforeDestroy() {
      // 清除所有与上传相关的 localStorage 记录
      localStorage.clear();
    }
  }
};
</script>

<style>
.fixed-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 9999;
}

.gradient-text {
  font-size: 30px;
  font-weight: bold;
  background: linear-gradient(to right, #1443a1, #8d94b2);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
  letter-spacing: 1px;
}

.el-header {
  background-color: #B3C0D1;
  color: #333;
  height: 80px;
}

.el-aside {
  background-color: #2c3e50;
  color: #ecf0f1;
  height: 714px;
}

.el-main {
  background-color: #f5f7fa;
  color: #333;
  height: 714px;
  transition: background-color 0.5s ease;
}

body > .el-container {
  margin-bottom: 40px;
}

/* 按钮悬停效果 */
.el-button:hover {
  transform: translateY(-1px);
  transition: all 0.2s ease-in-out;
}
.el-button:active {
  transform: scale(0.98);
}

/* 头像 hover 效果 */
.block:hover {
  transform: scale(1.1);
  transition: transform 0.2s;
}

/* 响应式适配 */
@media (max-width: 768px) {
  .el-aside {
    width: 100% !important;
    height: auto !important;
  }

  .el-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .el-row {
    flex-direction: column;
    align-items: flex-start !important;
  }
}
</style>